package com.example.common.security;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.web.mapper.UserMapper;
import com.example.web.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @program: springsecurity-study
 * @description: 实现身份认证
 * @author: ChenZhiXiang
 * @create: 2019-07-25 22:07
 **/
@Component
public class MyUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String s){
        // 根据用户名查询用户信息
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",s);
        User user = userMapper.selectOne(wrapper);
        if (null == user){
            throw new UsernameNotFoundException("用户不存在");
        }
        // 查找用户进行填充参数
        return new org.springframework.security.core.userdetails.User(user.getUsername(),passwordEncoder.encode(user.getPassword()),
                AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_qq"));
    }

//    public static void main(String[] args) {
//        System.out.println(new BCryptPasswordEncoder().encode("jayczx120"));
//    }
}
